<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use app\admin\controller\recharge\Api;
use app\admin\model\YOnlineorder;
use think\Exception;
use think\Log;
use fast\Http;
use function GuzzleHttp\json_decode;
use think\Config;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Gameorder extends Backend
{
    
    /**
     * YOnlineorder模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('YOnlineorder');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 与游戏服务器对账
     * @author violet
     * @since 2018-1-5
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            //搜索条件
            $filter = $this->request->get("filter", '');
            $filter_arr = json_decode($filter,true);
            $map = [];
            foreach ($filter_arr as $key=>$val){
                if($key == 'addtime'){
                    $time_arr = explode(',', $filter_arr['addtime']);
                    $star = $this->getMilliDate($time_arr[0].' 0:0:0');
                    $end = $this->getMilliDate($time_arr[1].' 23:59:59');
                    if(empty($time_arr[0])){
                        $map['addtime'] = array('<=', $end);
                    }elseif(empty($time_arr[1])){
                        $map['addtime'] = array('>=', $star);
                    }else{
                        $map['addtime'] = array('between', [$star,$end]);
                    }
                }else{
                    $map[$key] = $val;
                }
            }
            
            $total = $this->model
            ->where($map)
            ->where(['channel'=>2])
            ->order($sort, $order)
            ->count();
    
            $list = $this->model
            ->where($map)
            ->where(['channel'=>2])
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();
            
            if(empty($list)){
                $result = array("total" => $total, "rows" => $list);
                return json($result);
            }
            
            //处理数据
            $oid_arr = $orderID = $map_log = $map_user = $log_arr = $goods_arr = $user_arr = [];
            foreach ($list as $val){
                $orderID[] = $val['orderID'];
            }
            
            $game_list = Db::connect("GameDB")->table('recharge_record')->where(['orderid'=>array('in',$orderID)])->select();//游戏服充值记录
            foreach ($game_list as $val){
                $goods_arr[$val['orderid']] = $val;
            }
            foreach ($list as $key=>$val){
                $list[$key]['isgame'] = (isset($goods_arr[$val['orderID']])?'已入账':'未入账');
            }

            $result = array("total" => $total, "rows" => $list);
    
            return json($result);
        }
        return $this->view->fetch();
    }
    
    /**
     * 与游戏服通知接口
     */
    public function payNotice($orderID){
        
        $payData['outTradeNo'] = Config::get('Paykey.merchant').$orderID;
        $data = Http::post('https://www.darlingpage.com/payapi/query/detail.do',$payData);
        $data = json_decode($data,true);
        if($data['msgcode'] != 100){
            $this->error($data['msg']);
        }
        //订单详情
        $order_info = YOnlineorder::get(['orderID'=>$orderID,'status'=>0,'channel'=>2]);
        if(empty($order_info)){
            $this->error('无订单信息');
        }
        //支付状态
        if($data['msg']['tradeStatus'] != 'success'){
            $order_info->status = 1;
            if($order_info->save() === false){
                $this->error('支付失败,修改状态失败');
            }
            $this->error('支付失败');
        }
        //金额不等
        if(($order_info->orderamount) != $data['msg']['totalAmount']){
            $this->error('金额不对');
        }
        
        try
        {
            //添加房卡 并 修改订单信息 并 添加推广员提成
            $obj = new YOnlineorder();
            $obj->orderPay($orderID);
            $this->success('通知成功');
        }
        //捕获异常
        catch(Exception $e)
        {
            $this->error($e->getMessage());
        }
    }
    

}
